import Vue from 'vue'
import Vuex from 'vuex'
import { getSession, setSession, removeSession } from "@/utils/session";
Vue.use(Vuex)

const state = {
    curLibrary: JSON.parse(getSession("curLibrary"))
}
 
const getters = {
    curLibrary(state) {
        if (!state.curLibrary) return;
        return state.curLibrary;
    },
    libraryType(state) {
        if (!state.curLibrary) return;
        return state.curLibrary.type;
    },
    libraryName(state) {
        if (!state.curLibrary) return;
        return state.curLibrary.name;
    },
    libraryEnName(state) {
        if (!state.curLibrary) return;
        return state.curLibrary.enName;
    },
    libraryBgImg(state) {
        if (!state.curLibrary) return;
        return state.curLibrary.imgSrc;
    },
    libraryColor(state) {
        if (!state.curLibrary) return;
        return state.curLibrary.color;
    },
    libraryMeta(state) {
        if (!state.curLibrary) return;
        return state.curLibrary.meta;
    },
}

const actions = {
    setCourse({ commit }) {
        let curLibrary = JSON.parse(getSession("curLibrary"));
        commit('SETLIBRARY', curLibrary)
    },
}

const mutations = {
    SETLIBRARY: (state, d) => {
        if (d) {
            setSession("curLibrary", JSON.stringify(d))
        } else {
            removeSession("curLibrary")
        }
        state.curLibrary = d;
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}